package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.user.UserRegisterInfoErrorException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 注册用户信息
     *
     * @return 用户信息
     */
    @Log(title = "注册个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("registerProfile")
    public AjaxResult registerProfile(@RequestBody RegisterBody registerBody) {

        String msg = validateRegisterInfo(registerBody);
        msg = loginService.register(registerBody);

        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }


    private String validateRegisterInfo(RegisterBody registerBody) {

        if (StringUtils.isEmpty(registerBody.gettMallNumber())) {
            throw new UserRegisterInfoErrorException("tMallNumber");
        }

        if (StringUtils.isEmpty(registerBody.getPhoneNumber())) {
            throw new UserRegisterInfoErrorException("phoneNumber");
        }

        if (StringUtils.isEmpty(registerBody.getUsername())) {
            throw new UserRegisterInfoErrorException("username");
        }

        if (StringUtils.isEmpty(registerBody.getPassword())) {
            throw new UserRegisterInfoErrorException("password");
        }

        if (StringUtils.isEmpty(registerBody.getIdCard())) {
            throw new UserRegisterInfoErrorException("idCard");
        }

        if (StringUtils.isEmpty(registerBody.getEmail())) {
            throw new UserRegisterInfoErrorException("email");
        }

        String msg = "";

        if (registerBody.getUsername().length() < UserConstants.USERNAME_MIN_LENGTH
                || registerBody.getUsername().length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (registerBody.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH
                || registerBody.getPassword().length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }

        return msg;

    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
